package com.xingdata.jjxc.activity;

import jar.MessageManager;
import jar.model.RequestEntity;
import jar.tools.InstructionSet;

import java.util.ArrayList;
import java.util.List;

import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.widget.ImageView;
import android.widget.TextView;

import com.xingdata.jjxc.Common;
import com.xingdata.jjxc.R;
import com.xingdata.jjxc.activity.fragment.HudVideoFragment;
import com.xingdata.jjxc.activity.fragment.LocalVideoFragment;
import com.xingdata.jjxc.adapter.VideoPagerAdapter;
import com.xingdata.jjxc.utils.SharedPreTools;
import com.xingdata.jjxc.utils.SystemInfo;
import com.xingdata.jjxc.utils.ThreadSleep;
import com.xingdata.jjxc.utils.callback.ThreadNotifally;
import com.xingdata.jjxc.views.MyImageView;

public class VideoActivity extends FragmentActivity implements OnClickListener {
	private String TAG = "VideoActivity";
	private TextView title;
	private ViewPager viewPager;
	private List<Fragment> fragments = new ArrayList<Fragment>();
	private int[] viodelist;
	private Integer[] title_ids = { R.id.NetWork_Video, R.id.HUD_Video,
			R.id.Local_Video };
	private TextView networkGroups, hudGroups, localGroups;
	// 上一次点击ITEM
	private int currIndex = 0;
	private int bottomLineWidth;
	private int offset = 0;
	private int position_one;
	private int position_two;
	private int position_three;
	private ImageView ivBottomLine;
	private ProgressDialog progressDialog;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_video);
		
		((ImageView) findViewById(R.id.title_back)).setOnClickListener(this);
		title = (TextView) findViewById(R.id.title);
		networkGroups = (TextView) findViewById(R.id.NetWork_Video);
		networkGroups.setOnClickListener(new MyOnClickListener(0));
		hudGroups = (TextView) findViewById(R.id.HUD_Video);
		hudGroups.setOnClickListener(new MyOnClickListener(1));
		localGroups = (TextView) findViewById(R.id.Local_Video);
		localGroups.setOnClickListener(new MyOnClickListener(2));
		ivBottomLine = (ImageView) findViewById(R.id.iv_bottom_line);
		findViewById(R.id.title_capture).setOnClickListener(this);
		title.setText("行车记录仪");
		initdata();
		Log.d(TAG, "cursor imageview width=" + bottomLineWidth);
		DisplayMetrics dm = new DisplayMetrics();
		getWindowManager().getDefaultDisplay().getMetrics(dm);
		int screenW = dm.widthPixels;
		// offset = (int) ((screenW / 4.0 - bottomLineWidth) / 2);
		// Log.i("MainActivity", "offset=" + offset);

		position_one = (int) (screenW / 4.0);
		position_two = position_one * 2;
		position_three = position_one * 3;
	}

	private void initdata() {
		// fragments.add(new NetWorkVideoFragment());
		fragments.add(new HudVideoFragment());
		// fragments.add(new HudVieoFragment());
		// fragments.add(new HudVieoFragment());
		fragments.add(new LocalVideoFragment());
		viodelist = new int[] { 0, 1 };
		viewPager = (ViewPager) findViewById(R.id.viewPager);
		viewPager.setAdapter(new VideoPagerAdapter(getSupportFragmentManager(), fragments));
		viewPager.setCurrentItem(0);
		viewPager.setOnPageChangeListener(new MyOnPageChangeListener());
	}



	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.title_back:
			finish();
			break;

		case R.id.title_capture:
			showProgressDialog("提示", "正在请求抓拍");
			MessageManager.send(Integer.parseInt(SystemInfo
					.getHUD_System_Port()), new RequestEntity("",
					InstructionSet.INSTRUCTION_AUTH_CAPTURE_TO_HUD + ""));

			new ThreadSleep(Integer.parseInt(SharedPreTools.getString(this,
					Common.COF_CAPTURE, "3")) * 1000, new ThreadNotifally() {

				@Override
				public void beginStart() {
					// TODO Auto-generated method stub
					if (progressDialog != null && progressDialog.isShowing()) {
						progressDialog.dismiss();
					}
				}
			}).start();
			// RequestEntity entity = new RequestEntity();
			// entity.setMsg("1");
			// entity.setType(InstructionSet.INSTRUCTION_AUTH_GET_CARCORDER_TO_HUD
			// + "");
			// MessageManager.send(Integer.parseInt(SystemInfo.getSendPORT()),
			// entity);

			break;
		}

	}

	public class MyOnPageChangeListener implements OnPageChangeListener {

		@Override
		public void onPageSelected(int arg0) {
			Animation animation = null;
			switch (arg0) {
			case 0:
				if (currIndex == 1) {
					animation = new TranslateAnimation(position_one, 0, 0, 0);
					networkGroups.setTextColor(getResources().getColor(
							R.color.white));
				} else if (currIndex == 2) {
					animation = new TranslateAnimation(position_two, 0, 0, 0);
					hudGroups.setTextColor(getResources().getColor(
							R.color.white));
				} else if (currIndex == 3) {
					animation = new TranslateAnimation(position_three, 0, 0, 0);
					localGroups.setTextColor(getResources().getColor(
							R.color.white));
				}
				hudGroups.setTextColor(getResources().getColor(R.color.white));
				break;
			case 1:
				if (currIndex == 0) {
					animation = new TranslateAnimation(offset, position_one, 0,
							0);
					networkGroups.setTextColor(getResources().getColor(
							R.color.white));
				} else if (currIndex == 2) {
					animation = new TranslateAnimation(position_two,
							position_one, 0, 0);
					hudGroups.setTextColor(getResources().getColor(
							R.color.white));
				} else if (currIndex == 3) {
					animation = new TranslateAnimation(position_three,
							position_one, 0, 0);
					localGroups.setTextColor(getResources().getColor(
							R.color.white));
				}
				hudGroups.setTextColor(getResources().getColor(R.color.white));
				break;
			case 2:
				if (currIndex == 0) {
					animation = new TranslateAnimation(offset, position_two, 0,
							0);
					networkGroups.setTextColor(getResources().getColor(
							R.color.white));
				} else if (currIndex == 1) {
					animation = new TranslateAnimation(position_one,
							position_two, 0, 0);
					hudGroups.setTextColor(getResources().getColor(
							R.color.white));
				} else if (currIndex == 3) {
					animation = new TranslateAnimation(position_three,
							position_two, 0, 0);
					localGroups.setTextColor(getResources().getColor(
							R.color.white));
				}
				localGroups
						.setTextColor(getResources().getColor(R.color.white));
				break;
			}
			currIndex = arg0;
			animation.setFillAfter(true);
			// 设置动画速度
			animation.setDuration(200);
			ivBottomLine.startAnimation(animation);
		}

		@Override
		public void onPageScrolled(int arg0, float arg1, int arg2) {

		}

		@Override
		public void onPageScrollStateChanged(int arg0) {

		}

	}

	public class MyOnClickListener implements View.OnClickListener {
		private int index = 0;

		public MyOnClickListener(int i) {
			index = i;
		}

		@Override
		public void onClick(View v) {
			viewPager.setCurrentItem(index);
		}
	};

	/**
	 * 显示 ProgressDialog
	 */
	private ProgressDialog showProgressDialog(String title, String message) {
		if (progressDialog != null && progressDialog.isShowing()) {
			progressDialog.dismiss();
		}
		if (title == null) {
			title = "提示";
		}
		progressDialog = ProgressDialog.show(this, title, message);
		progressDialog.setCancelable(true);
		progressDialog
				.setOnCancelListener(new DialogInterface.OnCancelListener() {

					@Override
					public void onCancel(DialogInterface dialog) {
						dialog.dismiss();
					}
				});
		return progressDialog;
	}
	
}
